package com.gxa.hotel.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.hotel.admin.service.CouponInfoService;
import com.gxa.hotel.core.common.JsonResult;
import com.gxa.hotel.core.pojo.domain.CouponInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czj
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/couponInfo")
public class CouponInfoController {

    @Autowired
    CouponInfoService couponInfoService;

    private JsonResult jsonResult =null;

    @ApiOperation(value = "添加商品接口")
    @PostMapping("/add")
    /**
     * 添加优惠券的接口
     */
    public Map<String, Object> addCoupon(CouponInfo couponInfo){

//        String s = UUID.randomUUID().toString().replaceAll("-", "");
//        couponInfo.setCiAid(s);
        boolean update = couponInfoService.saveOrUpdate(couponInfo);

        jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(update);
        return jsonResult.getMap();

    }

    @ApiOperation(value = "根据id查询接口",produces = "query")
    @PostMapping("/selectById")
    /**
     * 根据id查询接口
     */
    public Map<String, Object> selectById(String id){
        CouponInfo byId = couponInfoService.getById(id);
        jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(byId);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "分页查询优惠券接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页查询数",dataType = "Integer",paramType = "query")
    })
    @PostMapping("/getCouponByPage")
    /**
     * 分页查询接口
     */
    public Map getCouponByPage(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size){

        Page<CouponInfo> page1 =new Page<>(page,size);
        Page<CouponInfo> page2 = couponInfoService.page(page1);
        jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(page2);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据id删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除的id",dataType = "Integer",paramType = "query")

    })
    @PostMapping("/deleteGoods")
    /**
     * 根据id删除接口
     */
    public Map deleteById(String id){

        boolean b = couponInfoService.removeById(id);
        jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(b);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "修改启用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponInfo",value = "修改的对象",dataType = "CouponInfo",paramType = "body")

    })
    @PostMapping("/updateState")
    /**
     * 修改上下架状态
     * @param couponInfo
     * @return
     */
    public Map updateState(CouponInfo couponInfo){
        boolean update = couponInfoService.saveOrUpdate(couponInfo);
        jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(update);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "查询的名字",dataType = "String",paramType = "query")

    })
    @PostMapping("/selectLike")
    /**
     * 根据优惠券名称模糊查询
     * @param name
     * @return
     */
    public Map selectLike(String name){
        LambdaQueryWrapper<CouponInfo> lambda= Wrappers.<CouponInfo>lambdaQuery();
        lambda.like(CouponInfo::getCiCouponName,name);
        List<CouponInfo> list = couponInfoService.list();
        jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(list);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }




}

